package com.bajiao.notify.controller.order;

import com.ijpay.core.kit.WxPayKit;
import com.bajiao.common.constant.CommonConstant;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.util.DateUtils;
import com.bajiao.entity.pay.PayNotifyData;
import com.bajiao.service.recharge.RechargeService;
import com.bajiao.third.entity.ConversionParams;
import com.bajiao.third.utils.AliPayUtils;
import com.bajiao.third.utils.WxPayUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 订单
 * @Author: FanJq
 * @Date: 2024-10-10-09-29
 * @Version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private WxPayUtils wxPayUtils;
    @Autowired
    private AliPayUtils aliPayUtils;
    @Autowired
    private RechargeService rechargeService;


    /**
     * 微信-充值回调
     *
     * @param request
     * @return
     */
    @RequestMapping("/wxPay/rechargeNotify")
    public String wxPayRechargeNotify(HttpServletRequest request) {
        Map<String, String> result = new HashMap<String, String>(2);
        try {
            Map<String, String> map = wxPayUtils.payNotify(request);
            if (!WxPayKit.codeIsOk(map.get("result_code"))) {
                result.put("return_code", "SUCCESS");
                result.put("return_msg", "OK");
                return WxPayKit.toXml(result);
            }
            String attach = "";
            if (StringUtils.isNotBlank(attach)) {
                attach = URLDecoder.decode(map.get("attach"));
            }
            PayNotifyData payNotifyData = PayNotifyData.PayNotifyDataBuilder.aPayNotifyData()
                    .type(CommonConstant.PAYMENT_TYPE_WX)
                    .params(attach)
                    .tradeNo(map.get("transaction_id"))
                    .outTradeNo(map.get("out_trade_no"))
                    .totalMoney(map.get("total_fee"))
                    .payTime(DateUtils.stringToDate(map.get("time_end"), "yyyyMMddHHmmss"))
                    .build();
            //调用更改订单状态的方法
            rechargeService.completePayment(payNotifyData);
        } catch (DataHandleException d) {
            if (d.getCode() == 0) {
                result.put("return_code", "FAIL");
                result.put("return_msg", d.getMessage());
                return WxPayKit.toXml(result);
            }
        }
        result.put("return_code", "SUCCESS");
        result.put("return_msg", "OK");
        return WxPayKit.toXml(result);
    }


    /**
     * 支付宝-充值回调
     *
     * @param request
     * @return
     */
    @RequestMapping("/aliPay/rechargeNotify")
    public String aliPayRechargeNotify(HttpServletRequest request) {
        try {
            Map<String, String> map = aliPayUtils.payNotify(request);
            ConversionParams conversionParams = new ConversionParams(map);
            //判断交易是否成功 如果失败直接返回 SUCCESS
            if (!StringUtils.equals("TRADE_SUCCESS", conversionParams.getTradeStatus())) {
                return "success";
            }
            PayNotifyData payNotifyData = PayNotifyData.PayNotifyDataBuilder.aPayNotifyData()
                    .type(CommonConstant.PAYMENT_TYPE_ALI)
                    .params(conversionParams.getPassbackParams())
                    .tradeNo(conversionParams.getTradeNo())
                    .outTradeNo(conversionParams.getOutTradeNo())
                    .totalMoney(conversionParams.getTotalAmount())
                    .payTime(DateUtils.stringToDate(conversionParams.getGmtPayment(), "yyyy-MM-dd HH:mm:ss"))
                    .build();
            //调用更改订单状态的方法
            rechargeService.completePayment(payNotifyData);
        } catch (DataHandleException d) {
            return "failure";
        }
        return "success";
    }
}
